package com.gf.pokemon.remote.oa;

import java.rmi.RemoteException;
import java.util.List;

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.client.Service;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

@org.springframework.stereotype.Service
public class OaService {
	
	private Logger logger = Logger.getLogger(OaService.class);

	@Value("${PENDING_URL_PREFIX}")
	private String PENDING_URL_PREFIX;

	@Value("${SYSTEM_SHORT_NAME}")
	private String SYSTEM_SHORT_NAME;

	@Value("${SYSTEM_NAME}")
	private String SYSTEM_NAME;

	@Value("${WSDL_USER}")
	private String WSDL_USER;

	@Value("${WSDL_PWD}")
	private String WSDL_PWD;

	@Value("${CREATE_TRANSACT_URL}")
	private String CREATE_TRANSACT_URL;

	@Value("${DEL_TEANSACT_URL}")
	private String DEL_TEANSACT_URL;

	/**
	 * 
	 * @param flowname
	 *            流程名称
	 * @param taskname
	 *            任务节点名称
	 * @param title
	 *            流程标题
	 * @param PreDocUser
	 *            推送该OA待办的人
	 * @param Listuser
	 *            被推送OA待办 处理事项的人
	 * @param uniqueFlag
	 *            待办的唯一标识
	 * @param amsUrl
	 *            跳转pokemon系统的链接地址
	 */
	public void pushOA(String flowname, String taskname, String title,
			String PreDocUser, List<String> Listuser, String uniqueFlag,
			String amsUrl) {
		Encryption et = new Encryption();
		String CurUserList = ""; // 推送待办要处理事项的人
		String countName = "";
		String strUrl = ""; // 跳转地址
		String url = "";
		try {
			for (int i = 0; i < Listuser.size(); i++) {
				countName = Listuser.get(i) == null ? "" : Listuser.get(i)
						.toString();
				url = PENDING_URL_PREFIX + amsUrl;
				if (i % 10 == 0) { // 按批次发
					if (i != 0) {
						Call call = getCreateCall();
						String ret = wsdlInvote(call, CurUserList.substring(0,
								CurUserList.length() - 1), strUrl.substring(0,
								strUrl.length() - 1), flowname, taskname,
								title, PreDocUser, uniqueFlag);
						System.out.println("result is " + et.Decode(ret));
					}
					CurUserList = "";
					strUrl = "";
					CurUserList = countName + "§";
					strUrl = addUserToUrl(url,countName) + "§";
				} else {
					CurUserList = CurUserList + countName + "§";
					strUrl = strUrl + addUserToUrl(url,countName) + "§";
				}
				if (i == Listuser.size() - 1) // 如果条数不足批次的条数时
				{

					Call call = getCreateCall();
					String ret = wsdlInvote(call,
							CurUserList.substring(0, CurUserList.length() - 1),
							strUrl.substring(0, strUrl.length() - 1), flowname,
							taskname, title, PreDocUser, uniqueFlag);
					logger.info("result is " + et.Decode(ret));

				}

			}

		} catch (Exception er) {
			logger.error(er.getMessage(),er);
		}
	}

	private String addUserToUrl(String url, String countName) {
		if(url==null){
			return null;
		}
		if(url.contains("?")){
			url += "&oaUser=" + countName;
		}else{
			url += "?oaUser=" + countName;
		}
		return url;
	}

	/**
	 * 关闭OA流程
	 * 
	 * @param flowname
	 * @param taskname
	 * @param PreDocUser
	 * @param uniqueFlag
	 */
	public void closeOA(String flowname, String taskname, String user,
			String uniqueFlag) {
		try {
			Call call = getCloseCall();
			Encryption et = new Encryption();
			String ret = et.Decode((String) call.invoke(new Object[] {
					new String(SYSTEM_NAME), new String(SYSTEM_SHORT_NAME),
					new String(flowname), new String(taskname),
					new String(uniqueFlag), new String(user),
					new String(WSDL_USER), new String(WSDL_PWD) }));
			logger.info("result is " + et.Decode(ret));

		} catch (Exception er) {
			logger.error(er.getMessage(),er);
		}
	}

	private String wsdlInvote(Call call, String pushPersons,
			String redirectUrl, String flowname, String taskname, String title,
			String preDocUser, String uniqueFlag) throws RemoteException {
		Encryption et = new Encryption();
		String ret = et.Decode((String) call.invoke(new Object[] {
				new String(SYSTEM_NAME), new String(SYSTEM_SHORT_NAME),
				new String(flowname), new String(taskname), new String(title),
				new String(preDocUser), new String(pushPersons),
				new String(uniqueFlag), new String(redirectUrl),
				new String(WSDL_USER), new String(WSDL_PWD) }));
		return ret;
	}

	private Call getCloseCall() throws ServiceException {
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(DEL_TEANSACT_URL);
		call.setOperationName("DelTransactDoc");
		call.addParameter("SystemName", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("SystemShortName", XMLType.XSD_STRING,
				ParameterMode.IN);
		call.addParameter("FlowName", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("CurProcess", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("SrcUnid", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("CurUser", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("User", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("Pwd", XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);
		return call;
	}

	/**
	 * 创建WSDL调用类
	 * 
	 * @return
	 * @throws ServiceException
	 */
	private Call getCreateCall() throws ServiceException {
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(CREATE_TRANSACT_URL);
		call.setOperationName("CreateTransactDoc");
		call.addParameter("SystemName", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("SystemShortName", XMLType.XSD_STRING,
				ParameterMode.IN);
		call.addParameter("FlowName", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("CurProcess", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("Subject", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("PreDocUser", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("CurUserList", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("SrcUnid", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("strUrl", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("User", XMLType.XSD_STRING, ParameterMode.IN);
		call.addParameter("Pwd", XMLType.XSD_STRING, ParameterMode.IN);
		call.setReturnType(XMLType.XSD_STRING);
		call.setUseSOAPAction(true);
		return call;
	}
}
